require("../css/commCss/normalize.less");
require("../css/commCss/reset.less");
require("../css/commCss/property.less");
require("../css/commCss/basic.less");
require("../fonts/iconfont.css");
require("../css/home.less");

require("../libs/swiper/swiper-bundle.css");

//axios请求
const axios = require("axios");

const Swiper = require("../libs/swiper/swiper-bundle.js");

//weui-插件库
require("weui");
const weui = require("weui.js");

//导入util
const util = require("../util/util");

document.addEventListener("DOMContentLoaded", function () {
  /* 获取元素 */
  const ranks = document.querySelector("#ranks");
  const punchDay = document.querySelector("#punchDay");
  const todayPunch = document.querySelector("#todayPunch");
  const badgee = document.querySelector("#badgee");

  /* 底部导航 */
  util.fixBottomNav("home");

  //1.轮播图
  function setSwiper() {
    //轮播图-初始化插件
    var mySwiper = new Swiper(".swiper", {
      autoplay: {
        delay: 1000, //延迟时间
        disableOnInteraction: false, //是否禁止自动滚动
      },

      loop: true, //循环播放
      pagination: {
        //分页器
        el: ".swiper-pagination",
        clickable: true, //允许点击
      },
    });
  }
  setSwiper();

  //2.首页数据渲染
  axios
    .get("http://139.9.177.51:8099/headPageInfo", {
      params: {
        userId: localStorage.getItem("userId"),
      },
    })
    .then(function (res) {
      if (res.data.status === 0) {
        ranks.textContent = res.data.data.rank;
        punchDay.textContent = res.data.data.punchIn;
        badgee.textContent = res.data.data.insigniaNum;

        //todayPunch.textContent = res.data.data.rank
        res.data.data.isPunch === "true"
          ? (todayPunch.textContent = "已打卡")
          : (todayPunch.textContent = "今日打卡");
      }
    });

  /* 3.今日打卡 */
  todayPunch.addEventListener("click", function () {
    axios
      .get("http://139.9.177.51:8099/clockIn", {
        params: {
          userId: localStorage.getItem("userId"),
        },
      })
      .then(function (res) {
        if (res.data.status === 0) {
          punchDay.textContent++;
          todayPunch.textContent = "已打卡";
          weui.toast("打卡成功", 1000);
        } else {
          weui.toast("已打卡", 1000);
        }
      });
  });
});
